<?php

namespace App\Models;

use App\Traits\External;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Report extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_report';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'agencyid',
        'mid',
        'otherid',
        'blacktype',
        'flag',
        'is_del'
    ];

    public function scopeNodel($query)
    {
        return $query->where('is_del', 'N');
    }

    protected $hidden = [
        'is_del',
    ];

    protected $appends = [
        'flagText','blacktypeText'
    ];

    public function getFlagTextAttribute()
    {
        if($this->flag == 'wait'){
            return '待处理';
        }elseif($this->status == 'pass'){
            return '情况属实，举报成功';
        }else{
            return '举报失败，证据不足';
        }
    }

    public function getBlacktypeTextAttribute()
    {
        if (!empty($this->blacktype)){
            $marryArr = External::getParams('blacktype');
            return $marryArr[$this->blacktype];
        }else{
            return '无';
        }
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function hasOneMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'mid');
    }

    public function hasOneOtherMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'otherid');
    }

    public static function getFilter($filter)
    {
        return self::whereHas('hasOneMember',function ($query) use ($filter){
            if (isset($filter['username']) && !empty($filter['username'])) {
                $query->where('username', 'LIKE', '%' . $filter['username'] . '%');
            }
        })->whereHas('hasOneOtherMember',function ($query) use ($filter){
            if (isset($filter['othername']) && !empty($filter['othername'])) {
                $query->where('username', 'LIKE', '%' . $filter['othername'] . '%');
            }
        })->where(function ($query) use ($filter){
            if (isset($filter['mid']) && !empty($filter['mid'])) {
                $query->where('mid',  $filter['mid'] );
            }
            if (isset($filter['otherid']) && !empty($filter['otherid'])) {
                $query->where('otherid',  $filter['otherid'] );
            }
            if (isset($filter['blacktype']) && !empty($filter['blacktype'])) {
                $query->where('blacktype',  $filter['blacktype'] );
            }
            if (isset($filter['flag']) && !empty($filter['flag'])) {
                $query->where('flag',  $filter['flag'] );
            }
        });
    }
}
